import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class View extends JFrame {
    private final static Sql SQL = new Sql("root", "123456", "test_book");
    private Connection connection;
    private JTextField search;
    private JButton selectButton;
    private JButton addButton;
    private JButton showButton;
    private JPanel labelPanel;
    private JPanel dataPanel;
    private JPanel searchPanel;

    public View() {
        setTitle("图书管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dataPanel = new JPanel();
        searchPanel();
        labelPanel();
        showData();


        selectButton.addActionListener(e -> searchBook());
        addButton.addActionListener(e -> addBook());
        showButton.addActionListener(e -> showData());

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(searchPanel, BorderLayout.NORTH);
        mainPanel.add(labelPanel, BorderLayout.CENTER);

        add(mainPanel,BorderLayout.NORTH);
        add(new JScrollPane(dataPanel),BorderLayout.CENTER);
        setVisible(true);
    }

    private void searchBook() {
        String name = search.getText();
        List<Book> books;

        try {
            connection = SQL.getConnection();
            books = SQL.selectStatement(connection, name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        updateDataPanel(books);
    }

    private void addBook() {
        Book book = getBook("新增图书");

        try {
            connection = SQL.getConnection();
            SQL.insertStatement(connection, book);
            showData();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void updateDataPanel(List<Book> books) {
        dataPanel.removeAll();
        if (books != null && !books.isEmpty()) {
            dataPanel.setLayout(new GridLayout(books.size(), 9));
            for (Book book : books) {
                JPanel bookPanel = addBookPanel(book);
                dataPanel.add(bookPanel);
            }
        } else {
            JLabel label = new JLabel("没有符合条件的书籍");
            label.setHorizontalAlignment(SwingConstants.CENTER);
            dataPanel.add(label);
        }
        dataPanel.revalidate();
        dataPanel.repaint();
    }

    private JPanel addBookPanel(Book book) {
        JPanel bookPanel = new JPanel(new GridLayout(1, 9));

        bookPanel.setBorder(new EmptyBorder(5, 10, 5, 10));
        bookPanel.setPreferredSize(new Dimension(bookPanel.getPreferredSize().width, 30)); // 设置高度为30像素

        JLabel idLabel = new JLabel(String.valueOf(book.getId()));
        JLabel nameLabel = new JLabel(book.getName());
        JLabel authorLabel = new JLabel(book.getAuthor());
        JLabel typeLabel = new JLabel(book.getType());
        JLabel priceLabel = new JLabel(String.valueOf(book.getPrice()));
        JLabel publishLabel = new JLabel(book.getPublish());
        JLabel statusLabel = new JLabel(book.getStatus() == 1 ? "可借阅" : "已借出");

        JButton deleteButton = new JButton("删除");
        JButton updateButton = new JButton("修改");

        idLabel.setHorizontalAlignment(SwingConstants.CENTER);
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        authorLabel.setHorizontalAlignment(SwingConstants.CENTER);
        typeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        priceLabel.setHorizontalAlignment(SwingConstants.CENTER);
        publishLabel.setHorizontalAlignment(SwingConstants.CENTER);
        statusLabel.setHorizontalAlignment(SwingConstants.CENTER);

        deleteButton.setPreferredSize(new Dimension(80, 30));
        updateButton.setPreferredSize(new Dimension(80, 30));

        bookPanel.add(idLabel);
        bookPanel.add(nameLabel);
        bookPanel.add(authorLabel);
        bookPanel.add(typeLabel);
        bookPanel.add(priceLabel);
        bookPanel.add(publishLabel);
        bookPanel.add(statusLabel);
        bookPanel.add(deleteButton);
        bookPanel.add(updateButton);

        deleteButton.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(this, "您确定要删除这本书吗？", "确认删除", JOptionPane.YES_NO_OPTION);
            if (confirm == JOptionPane.YES_OPTION) {
                deleteBook(book.getId());
                showData();
            }
        });

        updateButton.addActionListener(e -> {
            updateBook(book.getId());
        });

        return bookPanel;
    }

    private Book getBook(String title) {
        JTextField nameField = new JTextField(20);
        JTextField authorField = new JTextField(20);
        JTextField typeField = new JTextField(20);
        JTextField priceField = new JTextField(10);
        JTextField publishField = new JTextField(20);

        JPanel inputPanel = new JPanel(new GridLayout(5, 2));
        inputPanel.add(new JLabel("书名:"));
        inputPanel.add(nameField);
        inputPanel.add(new JLabel("作者:"));
        inputPanel.add(authorField);
        inputPanel.add(new JLabel("类型:"));
        inputPanel.add(typeField);
        inputPanel.add(new JLabel("价格:"));
        inputPanel.add(priceField);
        inputPanel.add(new JLabel("出版社:"));
        inputPanel.add(publishField);

        int result = JOptionPane.showConfirmDialog(null, inputPanel, title, JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String name = nameField.getText();
            String author = authorField.getText();
            String type = typeField.getText();
            double price = Double.parseDouble(priceField.getText());
            String publish = publishField.getText();

            Book book = new Book(name, author, type, price, publish);
            book.setStatus(1);

            return book;
        }
        return null;
    }

    private void showData() {
        dataPanel.removeAll();

        try {
            connection = SQL.getConnection();
            List<Book> books = SQL.showStatement(connection);
            updateDataPanel(books);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private void deleteBook(int id) {
        try {
            connection = SQL.getConnection();
            SQL.deleteClass(connection, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private void updateBook(int id) {
        Book book = getBook("修改图书");
        book.setId(id);

        try {
            connection = SQL.getConnection();
            SQL.updateStatement(connection, book);
            showData();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private void searchPanel() {
        search = new JTextField(30);
        selectButton = new JButton("搜索");
        addButton = new JButton("添加图书");
        showButton = new JButton("显示全部图书");

        searchPanel = new JPanel();
        searchPanel.add(search);
        searchPanel.add(selectButton);
        searchPanel.add(addButton);
        searchPanel.add(showButton);
    }

    private void labelPanel() {
        labelPanel = new JPanel(new GridLayout(1, 9));
        labelPanel.setBorder(new EmptyBorder(10, 10, 10, 10));

        String[] labels = {"ID", "书名", "作者", "类型", "价格", "出版社", "状态", "操作", "操作"};
        for (String label : labels) {
            JLabel jLabel = new JLabel(label);
            jLabel.setHorizontalAlignment(SwingConstants.CENTER);

            labelPanel.add(jLabel);
        }

        add(labelPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        new View();
    }
}